'''
新品图书
'''
import requests
from lxml import etree
import  simplejson
import io
import sys

#改变标准输出的默认编码
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')
DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"

headers = {'user-agent': DEFAULT_USER_AGENT}
url = 'https://book.douban.com/latest?icn=index-latestbook-all'
value_list=[]
response = requests.get(url,headers=headers)
text = response.text
html = etree.HTML(text,etree.HTMLParser())
img_url = html.xpath('//div[@id="content"]//li/a/img/@src')
detail = html.xpath('//div[@id="content"]//li/div[@class="detail-frame"]/p[3]/text()')
title= html.xpath('//div[@id="content"]//li//h2/a/text()')
detail_url = html.xpath('//div[@id="content"]//li/a[1]/@href')
publish =html.xpath('//div[@id="content"]//li//p[2]/text()')
score = html.xpath('//div[@id="content"]//li//span[2]/text()')
# starts = html.xpath('//div[@id="content"]//li//p[1]/span[1]')
for i in range(40):
    item_dict={
        'title':title[i].strip(),
        'score':score[i].strip(),
        'publish':publish[i].strip(),
        'detail':detail[i].strip(),
        'detail_url':detail_url[i].strip(),
        'img_url':img_url[i].strip()
    }
    value_list.append(item_dict)



from SqlHelper import MySqlHelper

if __name__ =='__main__':
    mysql = MySqlHelper()
    sql = "INSERT INTO douban(title,score,publish,detail,detail_url,img_url) values(%(title)s,%(score)s,%(publish)s,%(detail)s,%(detail_url)s,%(img_url)s)"
    data = [val for val in value_list]
    mysql.exec_many(sql,data)
